//树控件编号。
var _id = "mytree";
//树对象。
var _ztree = null;
//框架-子框架（操作的页面容器）
var _frame = "mainFrame";
var _menu;
var _menuName = "divMenu";
var _formTitleWidth="15%";
//单键ztree
function _ztreeClick()
{
	var _node=_ztree.getSelectedNode();
	if(0 == _node.id)
	{
		attachUrl("about:blank", _frame);
		return;
	}
	attachUrl(ctx+"/manage/sys/user/getDiffUser.htm?pid="+_node.id+"&r="+Math.random(), _frame);
	return false;
}
//右键
function _ztreeRClick(event, treeId, treeNode)
{
	if(treeNode.name == "未审核用户组")return;
	if(!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0)
	{
		_ztree.cancelSelectedNode();
		_showMenu("root", event.clientX, event.clientY);
	}
	else if(treeNode && !treeNode.noR)
	{
		_ztree.selectNode(treeNode);
		_showMenu("node", event.clientX, event.clientY);
	}
}
//隐藏菜单。
function _hideMenu()
{
	if(_menu){_menu.style.visibility = "hidden";}
}
//显示菜单
function _showMenu(type, x, y)
{
	$("#" + _menuName + " ul").show();
	$("#menu_refresh").show();
	$("#menu_adds").hide();
	$("#menu_add").hide();
	$("#menu_del").hide();
	var _node = _ztree.getSelectedNode();
	if(!_node)
	{
		$("#menu_refresh").hide();
		return;
	}
	if(0==_node.id)
	{
		$("#menu_refresh").show();
		$("#menu_adds").show();
	}
	else
	{
		if(_node.isParent)
		{
			$("#menu_add").show();
		}
		else
		{
			$("#menu_refresh").hide();
		}
		$("#menu_del").show();
	}
	$("#" + _menuName).css(
	{
		"top":y + "px", "left":x + "px", "visibility":"visible"
	});
}
//获得url。
function getUrl(treeNode)
{
	return ctx+"/manage/sys/user/getUserByPid.htm?pid="+treeNode.id;
}

_setting = {
	async: true,
	asyncUrl:getUrl,
	callback:
	{
		click:_ztreeClick,
		rightClick:_ztreeRClick
	},
	expandSpeed:""
};

$(function()
{
	//_setting.push({id:-1,deleteId:0,account:'',name:'未审核的用户',isParent:true,usertype:-1});
	_ztree = $("#"+_id).zTree(_setting,eval([{id:0,pid:-1,name:"用户列表",isParent:true}]));
	
	_menu = $("#" + _menuName)[0];
	$("body").mousedown(function(event)
	{
		if(!(event.target.id == _menuName || $(event.target).parents("#" + _menuName).length > 0))
		{
			_menu.style.visibility = "hidden";
		}
	});
	$("#divMenu ul").each(function(){
		$(this).click(function(){	
			$(this).parent().find("ul").removeClass("ul_highLight");
			$(this).addClass("ul_highLight");
		});
	});
	//右键菜单。
	$("#menu_refresh").click(function()
	{
		var _node = _ztree.getSelectedNode();
		_ztree.reAsyncChildNodes(_node, "refresh");
		_hideMenu();
	});
	$("#menu_adds").click(function()
	{
		var _node = _ztree.getSelectedNode();
		attachUrl(ctx+"/manage/sys/user/addUser1.htm?id=" + _node.id + "&status=0", _frame);
		_hideMenu();
	});
	$("#menu_add").click(function()
	{
		var _node = _ztree.getSelectedNode();
		attachUrl(ctx+"/manage/sys/user/addUser2.htm?id=" + _node.id + "&status=1", _frame);
		_hideMenu();
	});
	$("#menu_del").click(function()
	{
		var _node = _ztree.getSelectedNode();
		var _pre="";
		if(_node.isParent)
		{
			_pre="如果有子级，则无法删除，";
		}
		if(confirm(_pre+"是否删除？"))
		{
			$.post(ctx+"/manage/sys/user/delUserTree.htm",
			{
				id:_node.id
			}, function(data)
			{
				refresh_del(getTip(data));
			});
		}
		_hideMenu();
		return false;
	});
});
function refresh_add()
{
	var _node=_ztree.getSelectedNode();
	_node.isParent=true;
	_ztree.updateNode(_node);
	refresh();
}
function refresh_del(re)
{
	if(-1!=re.indexOf("成功"))
	{
		alert(re);
	}
	else if(re==2)
	{
		alert("有子级，不能删除！");
		return;
	}
	else if(-1!=re.indexOf("失败"))
	{
		alert(re);
		return;
	}
	else if(1==re)
	{
		alert("删除成功！");
	}
	else if(-1!=re.indexOf("子级"))
	{
		alert(re);
		return;
	}
	_ztree.removeNode(_ztree.getSelectedNode());
	attachUrl("about:blank", _frame);
	return;
}
function refresh_upd(txt)
{
	var _node=_ztree.getSelectedNode();
	_node.name=txt;
	_ztree.updateNode(_node);
}
function refresh_sort()
{
	refresh();
}
function refresh()
{
	var _node = _ztree.getSelectedNode();
	_ztree.reAsyncChildNodes(_node, "refresh");
	_hideMenu();
} 